package com.cbgolf.oa.activity.coupon;

import android.graphics.Matrix;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;
import com.cbgolf.oa.R;
import com.cbgolf.oa.adapter.MyPagerAdapter;
import com.cbgolf.oa.base.BaseActivity;
import com.cbgolf.oa.fragment.CouponFragment;
import java.util.ArrayList;
import java.util.List;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

/**
 * @author Created by lq on 2017/9/4.
 */

public class CouponActivity extends BaseActivity {

  //top
  @ViewInject(R.id.topBack_ll)
  private View topBack;
  @ViewInject(R.id.title_tv)
  private TextView topTitle;
  @ViewInject(R.id.topRight_iv)
  private ImageView topRightIv;

  //body
  @ViewInject(R.id.a_coupon_all_ll)
  private View allView;
  @ViewInject(R.id.a_coupon_allow_ll)
  private View pushedView;
  @ViewInject(R.id.a_coupon_refuse_ll)
  private View outDateView;
  //
  @ViewInject(R.id.a_coupon_all_tv)
  private TextView allTv;
  @ViewInject(R.id.a_coupon_allow_tv)
  private TextView pushedTv;
  @ViewInject(R.id.a_coupon_refuse_tv)
  private TextView outDateTv;
  //
  @ViewInject(R.id.a_coupon_line_ll)
  private View lineView;
  @ViewInject(R.id.a_coupon_line_iv)
  private ImageView lineIv;
  //viewpager
  @ViewInject(R.id.a_coupon_viewpager)
  private ViewPager viewPager;
  //
  @ViewInject(R.id.a_coupon_create_ll)
  private View createView;
  //var
  /**
   * 当前tab-index
   */
  private int currentIndex = 0;
  private int offSet = 0;

  private List<Fragment> listFragment = new ArrayList<>();

  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.a_coupon);
  }


  @Override
  protected void onRestart() {
    super.onRestart();
  }

  @Override
  protected void findViews() {
    x.view().inject(this);
  }

  @Override
  protected void setViews() {
    topTitle.setText("优惠券");
    topRightIv.setVisibility(View.VISIBLE);
    topRightIv.setImageResource(R.drawable.jilu);
    initLine();
    initViewPager();
  }

  private void initLine() {
    DisplayMetrics dm = new DisplayMetrics();
    getWindow().getWindowManager().getDefaultDisplay().getMetrics(dm);
    offSet = dm.widthPixels / 3;
    Matrix matrix = new Matrix();
    matrix.postTranslate(offSet, 0);
    lineIv.setImageMatrix(matrix);
    int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    outDateTv.measure(w, h);
    int width = outDateTv.getMeasuredWidth();
    ViewGroup.LayoutParams vl = lineIv.getLayoutParams();
    vl.width = width;
//        vl.height = vl.WRAP_CONTENT;
    lineIv.setLayoutParams(vl);
    int padding = (offSet - width) / 2;
    lineView.setPadding(padding, 0, padding, 0);
  }

  CouponFragment allFragment;
  CouponFragment pushedFragment;
  CouponFragment noPushedFragment;

  private void initViewPager() {
    allFragment = CouponFragment.newInstance("");
    pushedFragment = CouponFragment.newInstance(String.valueOf(true));
    noPushedFragment = CouponFragment.newInstance(String.valueOf(false));
    listFragment.add(0, allFragment);
    listFragment.add(1, pushedFragment);
    listFragment.add(2, noPushedFragment);
    MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager(), listFragment);
    viewPager.setAdapter(adapter);
    viewPager.setOffscreenPageLimit(1);
    viewPager.setCurrentItem(0);
    allFragment.onRefresh();

  }

  private final int ALL = 0;
  private final int ALLOW = ALL + 1;
  private final int REFUSH = ALLOW + 1;
  private boolean allowOnce = true;
  private boolean refuseOnce = true;

  @Override
  protected void setListener() {
    topBack.setOnClickListener(v -> finish());
    topRightIv.setOnClickListener(v -> mStartActivity(CouponHistoryActivity.class));
    createView.setOnClickListener(v -> mStartActivity(CouponCreateActivity.class));
    allView.setOnClickListener(v -> checkPage(ALL));
    pushedView.setOnClickListener(v -> checkPage(ALLOW));
    outDateView.setOnClickListener(v -> checkPage(REFUSH));
    viewPager.addOnPageChangeListener(new OnPageChangeListener() {
      int width = offSet;

      @Override
      public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

      }

      @Override
      public void onPageSelected(int position) {
        Animation animation = new TranslateAnimation(
            currentIndex * width, position * width, 0, 0);
        currentIndex = position;
        animation.setFillAfter(true);
        animation.setDuration(150);
        lineIv.startAnimation(animation);
        checkPage(position);
        if (position == ALLOW && null != pushedFragment && allowOnce) {
          pushedFragment.onRefresh();
          allowOnce = false;
        } else if (position == REFUSH && null != noPushedFragment && refuseOnce) {
          noPushedFragment.onRefresh();
          refuseOnce = false;
        }

      }

      @Override
      public void onPageScrollStateChanged(int state) {

      }
    });

  }

  private void checkPage(int index) {
    resetTextColor();
    switch (index) {
      case ALL:
        allTv.setTextColor(ContextCompat.getColor(context, R.color.bg_blue));
        break;
      case ALLOW:
        pushedTv.setTextColor(ContextCompat.getColor(context, R.color.bg_blue));
        break;
      case REFUSH:
        outDateTv.setTextColor(ContextCompat.getColor(context, R.color.bg_blue));
        break;
      default:
        break;
    }
    viewPager.setCurrentItem(index, true);
  }

  private void resetTextColor() {
    allTv.setTextColor(ContextCompat.getColor(context, R.color.text_gray));
    pushedTv.setTextColor(ContextCompat.getColor(context, R.color.text_gray));
    outDateTv.setTextColor(ContextCompat.getColor(context, R.color.text_gray));
  }
}
